<?
class FullTextSearchManager extends ORMManager {

	public function updateContentIndex($search_db,$params)
	{
		$qs = "
				update ".$search_db.".content_index
				   set title  = '".$params['title']."',
				       content = '".$params['content']."',
				       actual_time = '".$params['actual_time']."',
				       index_date = now()
			     where project_id = ".$params['project_id']." and
				       section_id = ".$params['section_id']." and
				       content_id = ".$params['content_id'];
		$this->execQuery($qs);
	}
	
	public function updateProjectSection($search_db,$params)
	{
		$qs = 'update '.$search_db.'.project_sections 
					set 
						last_indexed_id = '.$params['last_indexed_id'].' 
					where 
						project_id = '.$params['project_id'].' and 
						section_id = '.$params['section_id'];
		$this->dialog->execQuery($qs);
	}
	
	public function insertContentIndex($search_db, $params=array())
	{
		$qs = "
		insert into ".$search_db.".content_index
		   set id=null,
		       project_id = ".$params['project_id'].",
		       section_id = ".$params['section_id'].",
		       content_id = ".$params['content_id'].",
		       title  = '".$params['title']."',
		       content = '".$params['content']."',
		       actual_time = '".$params['actual_time']."',
		       index_date = now()";
		return $this->execQuery($qs);
	}
	
	public function getStopList($search_db)
	{
		$qs = "select word from ".$search_db.".stoplist";
		
		return $this->getArray($qs);
	}
	
	public function getContent($search_db, $keywords_opt)
	{
		$qs = "SELECT project_id, section_id, count(*) as cnt FROM ".$search_db.".content_index 
	    WHERE MATCH (title,content) AGAINST ('$keywords_opt' IN BOOLEAN MODE)
	    Group by project_id, section_id";
		return $this->getArray($qs);
	}
	
	public function getSearchResult($search_db, $project_id, $section_id, $keywords_opt, $start_item=0, $itemsPerPage=10)
	{
		$qs = "SELECT SQL_CALC_FOUND_ROWS *, date_format(actual_time, '%d.%m.%Y') as actual_time_short
	    FROM ".$search_db.".content_index 
	    WHERE project_id = $project_id and section_id = $section_id and
	    MATCH (title,content) AGAINST ('$keywords_opt' IN BOOLEAN MODE) ORDER by actual_time desc limit ".$start_item.','.$itemsPerPage;
		
		#print '<!-- ';
		#print $qs;
		#print ' -->';
		
		$data = $this->getArray($qs);
		
		$qs='SELECT FOUND_ROWS() cnt';
		$count_all = $this->getOneArray($qs);
		
		return array(data=>$data, count_all=>$count_all[cnt]);
	}
	
	public function insertKeyWordStat($obj)
	{
		$qs = "
			INSERT DELAYED INTO ".$obj->search_db.".keyword_stat
			   SET q_time       = now(),
			       user_ip      = ".$obj->dialog->remote_addr.",
			       q            = '".$obj->dialog->q."',
			       search_level = $obj->search_level,
			       q_cnt        = $obj->count,
			       project_id   = $obj->project_id,
			       section_id   = $obj->section_id
		";
		#print $qs;
		#print '<br>';
		$this->execQuery($qs);
	}
	
	public function insertIndexField($obj)
	{
		
	}
	
	public function getProject($search_db)
	{
		$qs = "select * from ".$search_db.".projects";
		#print $qs;
		#print '<br>';
		return $this->getArray($qs);
	}

	
	public function getProjectSection($search_db, $project_id)
	{
		$qs = "select * from ".$search_db.".project_sections where project_id = ".$project_id;
		#print $qs;
		#print '<br>';
		return $this->getArray($qs);
	}
}